/**
 * 
 */
package org.dse.html.processor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dse.html.TagName;
import org.dse.html.TagResultContainer;

/**
 * This class is used to process the title content.
 * 
 * It is configged in the application context and added into the
 * TagProcessorContainer singleton.
 * 
 * @author zhangchen
 * 
 */
public class TitleTagProcessor implements TagProcessor {

	private static final String regStr = "<title(>|\\s*.*>)(.*?)</title\\s*>";

	private static Pattern ptn = Pattern.compile(regStr,
			Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

	/**
	 * Process a title tag.
	 * 
	 * @param htmlContent
	 *            the whole content of current processed html document
	 * @param index
	 *            the start index of the current tag
	 * @param container
	 *            the TagResultContainer which the processor sends results to
	 */
	public void processTag(String htmlContent, int index,
			TagResultContainer container) {
		Matcher matcher = ptn.matcher(htmlContent);
		if (matcher.find(index)) {
			String titleContent = matcher.group(2);
			// add to result container with tag name "title"
			container.addResult(TagName.TITLE_TAG_NAME, titleContent);
		}
	}
}
